﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using DECOR.BL.BE;
using CM.DL.DALC.NC03;

namespace DECOR.DL.DALC.NC03
{
    public class UbigeoDALC : IUbigeoDALC
    {
        private string _connId = string.Empty;

        public static UbigeoDALC Instance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(UbigeoDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new UbigeoDALC(pConnId);
                    }
                }
            }
            return _instance;
        }
        private UbigeoDALC(string pConnId) { this._connId = pConnId; }
        private UbigeoDALC() { }
        private static volatile UbigeoDALC _instance = null;

        #region Miembros de IUbigeoDALC

        public List<DECOR.BL.BE.UbigeoBE> listUbigeos()
        {
            IRfcFunction function = null; RfcDestination dest = null;
            List<UbigeoBE> lst = new List<UbigeoBE>();
            UbigeoBE obj = null;
            DestConfig dc = new DestConfig();
            try
            {
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LISTA_UBIGEO");
                function.Invoke(dest);

                IRfcTable T_Ubigeo = function.GetTable("T_UBIGEO");
                foreach (IRfcStructure item in T_Ubigeo)
                {
                    obj = new UbigeoBE();
                    obj.UBIGEO = item.GetString("UBIGEO");
                    obj.UBI_DEP = item.GetString("UBI_DEP");
                    obj.UBI_PROV = item.GetString("UBI_PROV");
                    obj.UBI_DIST = item.GetString("UBI_DIST");
                    obj.UBI_NSE = item.GetString("UBI_NSE");

                    lst.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return lst;
        }

        #endregion
    }
}
